#import "PxCompositePickerView.h"
#import "Constants.h"

@implementation PxCompositePickerView

#pragma mark - Init
- (id)init
{
    self = [super init];
    if (self)
    {
        [self initialize];
    }
    return self;
}


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self initialize];
    }
    return self;
}


- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self)
    {
        [self initialize];
    }
    return self;
}


- (void)initialize
{
    _picker1 = [PxCustomPickerView new];
    _picker2 = [PxCustomPickerView new];
    _picker3 = [PxCustomPickerView new];
    [self addSubview:_picker1];
    [self addSubview:_picker2];
    [self addSubview:_picker3];
    
    _picker1.delegate = self;
    _picker2.delegate = self;
    _picker3.delegate = self;
    _picker1.tag = 2000;
    _picker2.tag = 2001;
    _picker3.tag = 2002;
    
//    _picker1.backgroundColor = [UIColor blackColor];
//    _picker2.backgroundColor = [UIColor redColor];
//    _picker3.backgroundColor = [UIColor yellowColor];
//    
//    self.backgroundColor = [UIColor blueColor];
    
    int padding = 16;
    [_picker1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.mas_centerY);
        make.left.equalTo(self);
        make.top.equalTo(self);
        make.height.equalTo(self);
    }];
    
    [_picker2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.mas_centerY);
        make.left.equalTo(_picker1.mas_right).with.offset(padding);
        make.top.equalTo(self);
        make.height.equalTo(self);
        make.width.equalTo(_picker1);
    }];
    
    [_picker3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.mas_centerY);
        make.left.equalTo(_picker2.mas_right).with.offset(padding);
        make.top.equalTo(self);
        make.height.equalTo(self);
        make.width.equalTo(_picker2);
        make.right.equalTo(self);
    }];
}

-(void)setData:(NSMutableArray *)arrays
{
    if (arrays.count != 3)
        return;
    [_picker1 setData:arrays[0] withMinIndex:0];
    [_picker2 setData:arrays[1] withMinIndex:1];
    [_picker3 setData:arrays[2] withMinIndex:1];
}

-(void)dataChange:(int)tag
{
    if (tag == 2000) {
        [_picker2 resetValue];
    } else if (tag == 2001) {
        [_picker3 resetValue];
    }
}

-(void)plusLastColumnValue
{
    [_picker3 plusValue];
}

-(NSString *)getSelectedValue
{
    return [NSString stringWithFormat:@"%@%@%@", [_picker1 getValue], [_picker2 getValue], [_picker3 getValue]];
}

-(void)setValue:(NSString *)areaCode
{
    NSString *code = [areaCode substringWithRange:NSMakeRange(0,2)];
    [_picker1 setValue:code];
    code = [areaCode substringWithRange:NSMakeRange(2,2)];
    [_picker2 setValue:code];
    code = [areaCode substringWithRange:NSMakeRange(4,2)];
    [_picker3 setValue:code];
}

@end